import _mssql

class C_SQLServer(object):
    def __init__(self, Server,user,password,database):
        self.Server=Server
        self.user=user
        self.password=password
        self.database=database
    #执行无返回操作，适用与(insert,update,delete)
    def execute_non_query(self,SQLStr):
        conn = _mssql.connect(server=self.Server, user=self.user, password=self.password, database=self.database,charset='utf8')
        conn.execute_non_query(SQLStr)
    #执行返回迭代器的操作，迭代器中的行以字典方式展示，适用于(select)
    def execute_query(self,SQLStr):
        conn = _mssql.connect(server=self.Server, user=self.user, password=self.password, database=self.database,charset='utf8')
        conn.execute_query(SQLStr)
        return conn
    #执行返回单行的字典
    def execute_row(self,SQLStr):
        conn = _mssql.connect(server=self.Server, user=self.user, password=self.password, database=self.database,charset='utf8')
        row=conn.execute_row(SQLStr)
        return row
    #执行返回单值的操作，适用于返回行计数等
    def execute_scalar(self,SQLStr):
        conn = _mssql.connect(server=self.Server, user=self.user, password=self.password, database=self.database,charset='utf8')
        scalar=conn.execute_scalar(SQLStr)
        return scalar
    #获取标题，以及标题类型字典
    def MSSQL_GetTitleDict(self,conn):

        titleDict={}
        for rows in conn.get_header():
            titleDict[rows[0]]=rows[1]
            #如果调用conn完成后千万记得，要吧连接关闭。
        conn.close()
        return titleDict


    